package com.first.community.service;

import com.first.community.dto.NotificationDTO;
import com.first.community.dto.PaginationDTO;
import com.first.community.enums.NotificationStatusEnum;
import com.first.community.enums.NotificationTypeEnum;
import com.first.community.exception.CustomizeErrorCode;
import com.first.community.exception.CustomizeExcetpion;
import com.first.community.mapper.NotificationMapper;
import com.first.community.model.Notification;
import com.first.community.model.NotificationExample;
import com.first.community.model.User;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    public PaginationDTO list(Long id, Integer page, Integer size) {


        PaginationDTO<NotificationDTO> paginationDTO = new PaginationDTO<>();
        NotificationExample notificationExample = new NotificationExample();
        notificationExample.createCriteria()
                .andReceiverEqualTo(id);
        //计算totalPage
        int totalCount = (int)notificationMapper.countByExample(notificationExample);
        //计算页面个数
        if(totalCount%size==0) {
            paginationDTO.setTotalPage(totalCount/size);
        }else{
            paginationDTO.setTotalPage(totalCount/size+1);
        }

        //page修正
        if(page<1){
            page=1;
        }
        if(page>paginationDTO.getTotalPage()){
            page=paginationDTO.getTotalPage();
        }
        Integer offset= size*(page-1);


        NotificationExample example1 = new NotificationExample();
        example1.createCriteria()
                .andReceiverEqualTo(id);
        example1.setOrderByClause("gmt_create desc");
        List<Notification> notifications=notificationMapper.selectByExampleWithRowbounds(example1,new RowBounds(offset,size));

        if(notifications.size()==0){
            return  paginationDTO;
        }
        List<NotificationDTO> notificationDTOS = new ArrayList<>();

        for (Notification notification : notifications) {
            NotificationDTO notificationDTO = new NotificationDTO();
             BeanUtils.copyProperties(notification,notificationDTO);
             notificationDTO.setTypeName(NotificationTypeEnum.nameOfType(notification.getType()));
             notificationDTOS.add(notificationDTO);
        }
        paginationDTO.setData(notificationDTOS);
        return paginationDTO;
    }

    public NotificationDTO read(Long id, User user) {

        Notification notification = notificationMapper.selectByPrimaryKey(id);
        if (notification==null){
            throw  new CustomizeExcetpion(CustomizeErrorCode.NOTIFICATION_NOT_FOUND);
        }
        if(!Objects.equals(notification.getReceiver(),user.getId())){
            throw  new CustomizeExcetpion(CustomizeErrorCode.READ_NOTIFICATION_FAIL);
        }
        notification.setStatus(NotificationStatusEnum.READ.getStatus());
        notificationMapper.updateByPrimaryKey(notification);

        NotificationDTO notificationDTO = new NotificationDTO();
        BeanUtils.copyProperties(notification,notificationDTO);
        notificationDTO.setTypeName(NotificationTypeEnum.nameOfType(notification.getType()));
        return notificationDTO;
    }

    public Long getUnreadCount(Long id) {
        NotificationExample example = new NotificationExample();
        example.createCriteria()
                .andReceiverEqualTo(id)
                .andStatusEqualTo(NotificationStatusEnum.UNREAD.getStatus());
        return  notificationMapper.countByExample(example);
    }
}
